import Dialog from '../../dist/dialog/dialog';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    available_point:0,
    info: {},
    count: 0,
    value: 0,
    mobile: "",
    point:""
  },
  onChange({detail}){
    this.setData({value:detail})
    if (this.data.info.out_rate !== 0 && this.data.info.in_rate !== 0) {
      let res = this.data.value * this.data.info.in_rate;
      res = res.toString()

      if(res.length>5){
        res = Number(res);
        res=res.toFixed(2)
      }

      this.setData({count:res})
      return
    }
    this.setData({count:0})
  },
  focus_input(e){
    if(e.detail.value == 0){
      this.setData({value:""})
    }
  },
  get_exchange() {
    app._getNetWork({
      url: app.getNetAddresss('plugin.point-exchange.frontend.exchange.index'),
      success: (res) => {
        let {result,msg,data} = res.data;
        if (result == 0) {
          return app.tips(msg)
        }

        if(data.member_mobile){
          let mobile = wx.getStorageSync('point_phone');
          if(mobile){
            this.setData({mobile})
          }else{
            this.setData({mobile:data.member_mobile})
          }
        }

        this.setData({
          info: data
        })
        wx.setNavigationBarTitle({
          title: `${data.name}`,
        })

      }
    })
  },
  handleAll() {
    this.setData({
      value:this.data.info.member_point
    })
  },
  submit() {
    Dialog.confirm({
        message: `
      ${this.data.value}个${this.data.point}将转兑到${this.data.info.another_name.shop}的${this.data.info.another_name.point}
      到账数量：${this.data.count}${this.data.info.another_name.point}
        手机号：${this.data.mobile}`,
      })
      .then(() => {
        this.confirm()
      })
      .catch(() => {
        // on cancel
      });
  },
  mobile_handle(e){
    this.setData({mobile:e.detail.value})
  },
  confirm(){
    let json = {
      point: this.data.value,
      mobile: this.data.mobile
    }
    app._getNetWork({
      url: app.getNetAddresss('plugin.point-exchange.frontend.exchange.exchange'),
      data:json,
      success: (res) => {
        let {result,msg,data} = res.data;
        if (result == 0) {
          return app.tips(msg)
        }
        this.setData({value:0})
        wx.setStorageSync('point_phone', this.data.mobile)
        app.tips(msg);
        if(data.mini_url){
          wx.navigateToMiniProgram({
            appId:data.mini_app_id,
            path:data.mini_url
          })
          // wx.navigateTo({
          //   url: `${data.mini_url}`,
          // })
        }
      

      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    const value = wx.getStorageSync('integral');
    this.setData({point:value})
    this.get_exchange()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
